package com.shigu.account.service;

import com.opentae.data.mall.beans.AdditionMoney;
import com.opentae.data.mall.beans.MallWallet;
import com.opentae.data.mall.beans.MallWalletRecord;
import com.opentae.data.mall.interfaces.AdditionMoneyMapper;
import com.opentae.data.mall.interfaces.MallWalletMapper;
import com.opentae.data.mall.interfaces.MallWalletRecordMapper;
import com.shigu.account.enums.MoneyRecordType;
import com.shigu.account.enums.WalletChangeType;
import com.shigu.account.exception.UtilsAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

//用户账户管理
@Service
public class UserAccountService {
    @Autowired
    MallWalletRecordMapper mallWalletRecordMapper;

    @Autowired
    MallWalletMapper mallWalletMapper;

    @Autowired
    AdditionMoneyMapper additionMoneyMapper;

    /**
     * 记录修改金额
     * @param oldMoney
     */
    private void changeMoneyRecord(Long userId,Double oldMoney,Double changeMoney,String remark,Integer type){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        MallWalletRecord record = new MallWalletRecord();
        record.setUserId(userId);
        record.setFromValue(oldMoney+"");
        record.setChangeValue(changeMoney+"");
        record.setChangeTime(sdf.format(new Date()));
        record.setRemark(remark);
        record.setType(type);
        mallWalletRecordMapper.insertSelective(record);
    }


    /**
     *
     * 用户增加余额
     * @param userId 用户ID
     * @param money 金额，必须为正数
     */
    public void addMoney(Long userId, Double money,String remark) throws UtilsAccountException {
        if (money<=0) {
            throw new UtilsAccountException("金额必须大于0");
        }
        MallWallet wallet = mallWalletMapper.selByUserId(userId);

        Double oldMoney = wallet.getMallMoney();
        mallWalletMapper.changeMoney(userId,money);
        changeMoneyRecord(userId,oldMoney,money,remark, WalletChangeType.money.getValue());
    }

    /**
     * 用户减余额
     * @param userId 用户ID
     * @param money 金额，必须为正数
     */
    public void reduceMoney(Long userId, Double money,String remark) throws UtilsAccountException {
        if (money<=0) {
            throw new UtilsAccountException("金额必须大于0");
        }
        MallWallet wallet = mallWalletMapper.selByUserId(userId);

        Double oldMoney = wallet.getMallMoney();
        if (oldMoney<money || mallWalletMapper.changeMoney(userId,-money)==0) {
            throw new UtilsAccountException("余额不足");
        }
        changeMoneyRecord(userId,oldMoney,-money,remark,WalletChangeType.money.getValue());
    }

    /**
     *
     * @description 奖励用户
     * @author 郭尔
     * @date 2019-06-13 10:15
     */
    public void rewardUser(Long userId,String money) throws UtilsAccountException {
        addMoney(userId,Double.valueOf(money), MoneyRecordType.bonus.getText());
        mallWalletMapper.changeTotal(userId,Double.valueOf(money));
        recordAddtionMoney(userId,1,Double.valueOf(money));
    }


    /**
     *
     * @description 用户增加抵用券
     * @author 郭尔
     * @date 2019-11-12 10:18
     */
    public void addVoucher(Long userId, Double voucher,String remark) throws UtilsAccountException {
        if (voucher<=0) {
            throw new UtilsAccountException("金额必须大于0");
        }
        MallWallet wallet = mallWalletMapper.selByUserId(userId);

        Double oldVoucher = wallet.getVoucher();
        mallWalletMapper.changeVoucher(userId,voucher);
        changeMoneyRecord(userId,oldVoucher,voucher,remark, WalletChangeType.voucher.getValue());
    }

    /**
     *
     * @param userId 用户ID
     * @param voucher 抵用券金额
     */
    public void reduceVoucher(Long userId, Double voucher,String remark) throws UtilsAccountException {
        if (voucher<=0) {
            throw new UtilsAccountException("金额必须大于0");
        }
        MallWallet wallet = mallWalletMapper.selByUserId(userId);

        Double oldVoucher = wallet.getVoucher();
        if (oldVoucher < voucher || mallWalletMapper.changeVoucher(userId,-voucher)==0) {
            throw new UtilsAccountException("抵用券余额不足");
        }
        changeMoneyRecord(userId,oldVoucher,-voucher,remark,WalletChangeType.voucher.getValue());
    }

    /**
     * @param type 1 余额 2抵用券
     * @description 金钱记录
     * @author 郭尔
     * @date 2019-11-20 13:55
     */
     public void recordAddtionMoney(Long userId,int type,Double money){
         AdditionMoney record = new AdditionMoney();
         record.setMallUserId(userId);
         record.setMoney(money);
         record.setOperateTime(new Date());
         record.setType(type);
         additionMoneyMapper.insertSelective(record);
     }

}
